package com.fs.system.sys_admin.service;

import cn.hutool.core.util.ObjectUtil;
import com.fs.system.college_admin.domain.CollegeAdmin;
import com.fs.system.exception.CustomerException;
import com.fs.system.manager.domain.Account;
import com.fs.system.sys_admin.controller.SysAdminController;
import com.fs.system.sys_admin.domain.SysAdmin;
import com.fs.system.sys_admin.mapper.SysAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class SysAdminService {

    @Resource
    private SysAdminMapper sysAdminMapper;

    /**
     * 系统管理员登录
     * @param account
     * @return
     */
    public Map<String, Object> login(Account account) {
        Map<String, Object> resMap = new HashMap<>();
        SysAdmin dbSysAdmin = sysAdminMapper.selectByUsername(account.getUsername());
        if(Objects.isNull(dbSysAdmin)){
            throw new CustomerException("500","用户名不存在");
        }
        if(!"0".equals(dbSysAdmin.getStatus())){
            throw new CustomerException("500","用户状态不合法");
        }
        //用户存在
        //判断密码是否正确
        if(!dbSysAdmin.getPassword().equals(account.getPassword())){
            throw new CustomerException("500","用户名或密码错误");
        }
        //所有逻辑判断没有问题，返回前端所需
        resMap.put("sysId", dbSysAdmin.getSysId());
        resMap.put("username", dbSysAdmin.getUsername());
        resMap.put("nickname", dbSysAdmin.getNickname());
        resMap.put("status", dbSysAdmin.getStatus());
        resMap.put("role", dbSysAdmin.getRole());
        return resMap;
    }

    /**
     * 修改密码
     * @param account
     */
    public void updatePassword(Account account) {
        /*从数据库拿出对象*/
        SysAdmin dbSysAdmin = sysAdminMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbSysAdmin)) {
            throw new CustomerException("500","用户不存在");
        }
        /*判断原本输入传过来的原密码和查到的用户在数据库的密码是否一致*/
        if (!account.getPassword().equals(dbSysAdmin.getPassword())) {
            throw new CustomerException("500","原密码错误");
        }
        dbSysAdmin.setPassword(account.getNewPassword());
        dbSysAdmin.setUpdateBy(account.getUsername());
        dbSysAdmin.setUpdateTime(LocalDateTime.now());
        /*将更新过的对象再更新到数据库里面*/
        sysAdminMapper.update(dbSysAdmin);
    }

    /**
     * 修改系统管理员信息
     * @param sysAdmin
     * @return
     */
    public void updateSysAdmin(SysAdmin sysAdmin) {
        Long sysId = sysAdmin.getSysId();
        String username = sysAdmin.getUsername();
        SysAdmin dbSysAdmin1 = sysAdminMapper.selectBySysId(sysId);
        if(!dbSysAdmin1.getUsername().equals(username)){
            SysAdmin dbSysAdmin2 = sysAdminMapper.selectByUsername(username);
            if(Objects.nonNull(dbSysAdmin2)){
                throw new CustomerException("500","用户已存在");
            }
        }
        sysAdmin.setUpdateBy(sysAdmin.getUpdateBy());
        sysAdmin.setUpdateTime(LocalDateTime.now());
        sysAdminMapper.update(sysAdmin);
    }

    /**
     * 查询所有用户
     * @param sysAdmin
     * @return
     */
    public List<SysAdmin> selectAll(SysAdmin sysAdmin) {
        List<SysAdmin> sysAdmins = sysAdminMapper.selectAllAdmin(sysAdmin);
        return sysAdmins;
    }

    /**
     * 分页查询用户
     * @param sysAdmin
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<SysAdmin> selectPage(SysAdmin sysAdmin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysAdmin> sysAdmins = sysAdminMapper.selectAllAdmin(sysAdmin);
        return PageInfo.of(sysAdmins);
    }

    /**
     * 添加系统管理员
     * @param sysAdmin
     */
    public void addSysAdmin(SysAdmin sysAdmin) {
        SysAdmin dbsysAdmin = sysAdminMapper.selectByUsername(sysAdmin.getUsername());
        if (Objects.nonNull(dbsysAdmin)) {
            throw new CustomerException("500","用户已存在");
        }
        sysAdmin.setPassword(sysAdmin.getUsername());
        sysAdmin.setCreateTime(LocalDateTime.now());
        sysAdmin.setDeleted(false);
        sysAdmin.setRole("SUPER_ADMIN");
        sysAdminMapper.addSysAdmin(sysAdmin);
    }

    /**
     * 修改管理员信息
     * @param sysId
     * @param updatedBy
     */
    public void deleteSysAdmin(Long sysId, String updatedBy) {
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setSysId(sysId);
        sysAdmin.setDeleted(true);
        sysAdmin.setUpdateBy(updatedBy);
        sysAdmin.setUpdateTime(LocalDateTime.now());
        sysAdminMapper.update(sysAdmin);
    }

    /**
     * 批量删除管理员
     * @param ids
     * @param updatedBy
     */
    public void deleteBatch(List<Long> ids, String updatedBy) {
        for (Long sysId :ids) {
            this.deleteSysAdmin(sysId, updatedBy);
        }
    }
}
